package com.ax.api.route.base.drugstore;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.ax.api.route.util.RouteConfig;

@RestController
@RequestMapping("/drugstoreManager")
public class DrugstoreManagerRoute {

	 @Autowired
	 LoadBalancerClient loadBalancerClient;
	 @Autowired
	 RestTemplate restTemplate;
	 
	 
	/**
	 * 菜单页面的跳转
	 */
	@GetMapping("/sys/drugstoreManager")
	String drugAdministration(Model model) {
	   String url = geturl("/sys/drugstoreManager");
	   return restTemplate.getForObject(url, String.class);
	}
	
	/**
	 * queryDrugStoreManager
	 * remote_base
	 * 药房人员查询(模块:药店人员)
	 */ 
	@GetMapping("/queryDrugStoreManager")
	public String queryDrugclass(@RequestParam Map<String,Object> map) {
		String value = "?personnelId={personnelId}&offset={offset}&drugstoreId={drugstoreId}&limit={limit}";
		String url = geturl("/sys/drugstoreManager/queryDrugStoreManager") + value;
		return restTemplate.getForObject(url, String.class,map);
}
	
	/**
	 * add
	 * remote_base
	 * 药房人员增加(模块:药店人员)*跳转增加页面
	 */   
	@GetMapping("/add/drugstoreManager")
	public String add(){
	    String url = geturl("/sys/drugstoreManager/add");
		return restTemplate.getForObject(url, String.class);
	}
	
	/**
	 * insertBasePersonnel
	 * remote_base
	 * 药品分类增加(模块:药品分类)
	 */ 
	@GetMapping("/insertBasePersonnel")
	public String insertBaseDrugClass(@RequestParam Map<String,Object> map) {
		String value = "?personnelId={personnelId}&personnelName={personnelName}&drugstoreId={drugstoreId}&drugstoreName={drugstoreName}&typeCode={typeCode}&typeName={typeName}&personId={personId}&empno={empno}&sex={sex}&birth={birth}&entryTime={entryTime}&phone={phone}&address={address}&remarks={remarks}&flag={flag}";
		String url = geturl("/sys/drugstoreManager/insertBasePersonnel") + value;
		return restTemplate.getForObject(url, String.class,map);
    }
	
	/**
	 * edit
	 * remote_base
	 * 药方人员修改(模块:药店人员)*跳转修改页面
	 */   
	@GetMapping("/edit/drugstoreManager")
	public String edit(String id){
		String value = "/id=" + id;
	    String url = geturl("/sys/drugstoreManager/edit") + value;
		return restTemplate.getForObject(url,String.class,id);
	}
	
	/**
	 * updateBasePersonnel
	 * remote_base
	 * 药房修改(模块:药房管理)
	 */ 
	@GetMapping("/updateBasePersonnel")
	public String updateBasePersonnel(@RequestParam Map<String,Object> map) {
		String value = "?personnelId={personnelId}&personnelName={personnelName}&drugstoreId={drugstoreId}&drugstoreName={drugstoreName}&typeCode={typeCode}&typeName={typeName}&personId={personId}&empno={empno}&sex={sex}&birth={birth}&entryTime={entryTime}&phone={phone}&address={address}&remarks={remarks}&flag={flag}";
		String url = geturl("/sys/drugstoreManager/updateBasePersonnel") + value;
		return restTemplate.getForObject(url, String.class,map);
  }
	
		private String geturl(String path){
			 ServiceInstance serviceInstance = loadBalancerClient.choose(RouteConfig.BASE_ROUTE);
			 StringBuffer bf = new StringBuffer("http://");
			 bf.append(serviceInstance.getHost());
			 bf.append(":");
			 bf.append(serviceInstance.getPort());
			 bf.append(path);
			 return bf.toString();
		   }
}
